package cn.com.fssg.platform.base.handler.impl.gsl.lz;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 工商联履职发布接口
 */
@Component
public class GslLzPushCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        Map<String, Object> updateMap = new HashMap<>();
        updateMap.put("id", jsonObject.getString("id"));
        updateMap.put("state", "1");
        updateMap.put("end_date", jsonObject.getString("end_date"));
        String qSql = "select * from gsl_assessment_log where ass_id=:id";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(qSql, updateMap);
        int sCount = 1;
        if(!maps.isEmpty()){
            sCount = maps.size() + 1;
        }
        updateMap.put("count", sCount);
        //修改状态
        String sSql = "update gsl_assessment set state=:state, count=:count, end_date=:end_date where id=:id";
        jdbcTemplate.update(sSql, updateMap);


        Map<String, Object> params = new HashMap<>();
        params.put("ass_id", jsonObject.getString("id"));
        params.put("update_by", BizUtil.cur().curUserId());
        params.put("update_date", new Date());
        params.put("create_by", BizUtil.cur().curUserId());
        params.put("create_date", new Date());
        params.put("del_flag", "0");
        params.put("name", BizUtil.cur().curLoginname());
        params.put("end_date", jsonObject.getString("end_date"));
        params.put("id", BizUtil.cur().getSnowflakeIdStr());
        String sql = "insert into gsl_assessment_log (id, ass_id, update_by, update_date, create_by, create_date, del_flag, name, end_date) values " +
                "(:id, :ass_id, :update_by, :update_date, :create_by, :create_date, :del_flag, :name, :end_date)";
        jdbcTemplate.update(sql, params);
        return null;
    }
}